#requires -version 2.0 
Function Get-NaLicense {
	<#
	.Synopsis
    	Returns information about the current list of licensed Data ONTAP services
    .Description
       	Returns information about the current list of licensed Data ONTAP services, 
	 	their codes, the type of license, and, if it is a time limited license, the
	 	expiration date. It also tells the services that are not licensed for your 
	 	appliance, or if a time limited licensed service has expired.
	.Parameter Name
        Name of the Data ONTAP Service.
    .Parameter Licensed
        Will return every service which is currently licensed.
    .Parameter Server
        NaServer to query
	.Example
		# get the license information for cifs
       	PS > Get-NaLicense -name cifs
    .Example
		# Return every licensed service
       	PS > Get-NaLicense -Licensed
	.Outputs
    	NetApp.SDK.NaLicense[]
    .Link
    	Add-NaLicense
    	Remove-NaLicense
	#>
	[CmdletBinding(SupportsShouldProcess=$false,SupportsTransactions=$False,ConfirmImpact="none",DefaultParameterSetName="")]
    param (		
    	[Parameter(Position=0,ValueFromPipelineByPropertyName=$TRUE,HelpMessage="Name of the Data ONTAP service.")]
		[Alias("Service")]
		[string] 
		$name,
	
		[Parameter()]
		[switch]
		$Licensed,

		[Parameter(HelpMessage="NaServer to query")]
		[NetApp.SDK.NaServer] 
		$server = (Get-NaServer)
	)
	process{
		$out = @()
		$request = New-Object NetApp.Manage.NaElement -ArgumentList @("license-list-info")
		try {
			$result  = ([xml]$server.InvokeElem($request)).results
		}
		catch [NetApp.Manage.NaApiFailedException]{
			Write-Warning $_.Exception.GetBaseException().message
			$result = $null
			continue;
		}
		if ($?){
			$result."licenses"."license-info" |% {
				$out += New-Object NetApp.SDK.NaLicense -ArgumentList @(
					$_."count",
					(ConvertFrom-NaBool $_."is-demo"),
					(ConvertFrom-NaBool $_."is-expired"),
					(ConvertFrom-NaBool $_."is-licensed"),
					(ConvertFrom-NaBool $_."is-site"),
					$_."service",
					$_."code",
					(ConvertFrom-NaTime $_."expiration-timestamp" -UTC),
					(ConvertFrom-NaTime $_."installation-timestamp" -UTC),
					$_."length",
					$_."node-count",
					$_."platform",
					$_."storage-count"
				)
			}
			if ($name){
				Write-Output $out | ?{$_.Service -match $name}
			} 
			elseif ($Licensed){
				Write-Output $out | ?{($_.Licensed)}
			}
			else{
				Write-Output $out			
			}
		}
	}
}
function Add-NaLicense {
    <#
    .Synopsis
      	Enable license for a Data ONTAP service.
    .Description
       	Enable license for a Data ONTAP service.
	.Parameter Code
        license code of a Data ONTAP service to be enabled.
	.Parameter Server
        NaServer to query
	.Example
		# Add a license to your filer.
       	PS > Add-NaLicense DZDACHD
    .Outputs
    	NetApp.SDK.NaLicense[]
    .Link
    	Add-NaLicense
    	Get-NaLicense
	#>
	[CmdletBinding(SupportsShouldProcess=$true,SupportsTransactions=$False,ConfirmImpact="low",DefaultParameterSetName="")]
    param (
		[Parameter(Position=0,ValueFromPipelineByPropertyName=$True,Mandatory=$TRUE,HelpMessage="license code of a Data ONTAP service to be enabled.")]
		[string]
		$code,

		[Parameter(Position=1,HelpMessage="NaServer to query")]
		[NetApp.SDK.NaServer] 
		$server = (Get-NaServer)
    )
	Process{
        $before = New-Object hashtable
		#load the current liceses into a hashtable for comparison later.
		Get-NaLicense -Server $server| %{$before.Add($_.service, $_.licensed)}    
		$request = New-Object NetApp.Manage.NaElement -ArgumentList @("license-add")
		$request.AddNewChild("code",$code)
		if ($pscmdlet.ShouldProcess($server.Server, "Add License $($code)")){
			try {
				$server.InvokeElem($request) | out-null
				$pscmdlet.writeverbose("License Added")
			}
			catch [NetApp.Manage.NaApiFailedException]{
				Write-Warning $_.Exception.GetBaseException().message
				continue;
			}
			if ($?){
				# Find the license we just added!
				return Get-Nalicense -Server $server | ? {$_.Licensed -ne $before.$($_.service)}
			}
        }
	}
}
Function Remove-Nalicense {
	<#
	.Synopsis
        Disable license for a Data ONTAP service.
    .Description
        Disable license for a Data ONTAP service.
	.Parameter Name
        Name of the Data ONTAP service to be disabled.
	.Parameter Server
        NaServer to query
	.Example
		# Disable CIFS on your filer
       Remove-NaLicense cifs
    .Outputs
    	NetApp.SDK.NaLicense[]
    .Link
    	Add-NaLicense
    	Get-NaLicense
	#>
	[CmdletBinding(SupportsShouldProcess=$true,SupportsTransactions=$False,ConfirmImpact="low",DefaultParameterSetName="")]
    param (		
		[Parameter(Position=0,Mandatory=$TRUE,ValueFromPipelineByPropertyName=$TRUE,HelpMessage="Name of the Data ONTAP service to be disabled.")]
		[Alias("Service")]
		[string] 
		$name,
		
		[Parameter(Position=1,HelpMessage="NaServer to query")]
		[NetApp.SDK.NaServer] 
		$server = (Get-NaServer)
    )
	
	process {
		$request = New-Object NetApp.Manage.NaElement -ArgumentList @("license-delete")
		$request.AddNewChild("service",$name)
		if ($pscmdlet.ShouldProcess($server.server, "Remove License: $name")){
			try {
				$server.InvokeElem($request)|out-null
			}
			catch [NetApp.Manage.NaApiFailedException]{
				Write-Warning $_.Exception.GetBaseException().message
				continue;
			}
			if ($?){
				return Get-NaLicense -name $name -server $Server
			}
        } 
	}
}
